home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_xemacs.idb / usr / freeware / lib / xemacs-20.4 / lisp / prim / indent.el.z / indent.el
Encoding:
Text File  |  1998-05-21  |  17.5 KB  |  479 lines

  1. ;;; indent.el --- indentation commands for XEmacs
  2.  
  3. ;; Copyright (C) 1985, 1992, 1993, 1995, 1997 Free Software Foundation, Inc.
  4.  
  5. ;; Maintainer: FSF
  6. ;; Keywords: lisp languages tools
  7.  
  8. ;; This file is part of XEmacs.
  9.  
  10. ;; XEmacs is free software; you can redistribute it and/or modify it
  11. ;; under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation; either version 2, or (at your option)
  13. ;; any later version.
  14.  
  15. ;; XEmacs is distributed in the hope that it will be useful, but
  16. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  18. ;; General Public License for more details.
  19.  
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with XEmacs; see the file COPYING.  If not, write to the Free
  22. ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  23. ;; 02111-1307, USA.
  24.  
  25. ;;; Synched up with: FSF 19.30.
  26.  
  27. ;;; Commentary:
  28.  
  29. ;; Commands for making and changing indentation in text.  These are
  30. ;; described in the XEmacs Reference Manual.
  31.  
  32. ;; 06/11/1997 - Convert (preceding|following)-char to char-(before|after) -slb
  33.  
  34. ;;; Code:
  35.  
  36. (defvar standard-indent 4 "\
  37. Default number of columns for margin-changing functions to indent.")
  38.  
  39. (defvar indent-line-function 'indent-to-left-margin
  40.   "Function to indent current line.")
  41.  
  42. (defun indent-according-to-mode ()
  43.   "Indent line in proper way for current major mode."
  44.   (interactive)
  45.   (funcall indent-line-function))
  46.  
  47. (defun indent-for-tab-command (&optional prefix-arg)
  48.   "Indent line in proper way for current major mode."
  49.   (interactive "P")
  50.   (if (eq indent-line-function 'indent-to-left-margin)
  51.       (insert-tab prefix-arg)
  52.     (if prefix-arg
  53.     (funcall indent-line-function prefix-arg)
  54.       (funcall indent-line-function))))
  55.  
  56. (defun insert-tab (&optional prefix-arg)
  57.   (let ((count (prefix-numeric-value prefix-arg)))
  58.     (if abbrev-mode
  59.     (expand-abbrev))
  60.     (if indent-tabs-mode
  61.     (insert-char ?\t count)
  62.       ;; XEmacs: (Need the `1+')
  63.       (indent-to (* tab-width (1+ (/ (current-column) tab-width)))))))
  64.  
  65. (defun indent-rigidly (start end arg)
  66.   "Indent all lines starting in the region sideways by ARG columns.
  67. Called from a program, takes three arguments, START, END and ARG."
  68.   (interactive "r\np")
  69.   (save-excursion
  70.     (goto-char end)
  71.     (setq end (point-marker))
  72.     (goto-char start)
  73.     (or (bolp) (forward-line 1))
  74.     (while (< (point) end)
  75.       (let ((indent (current-indentation))
  76.         eol-flag)
  77.     (save-excursion
  78.       (skip-chars-forward " \t")
  79.       (setq eol-flag (eolp)))
  80.     (or eol-flag
  81.         (indent-to (max 0 (+ indent arg)) 0))
  82.     (delete-region (point) (progn (skip-chars-forward " \t") (point))))
  83.       (forward-line 1))
  84.     (move-marker end nil)
  85.     (setq zmacs-region-stays nil))) ; XEmacs
  86.  
  87. (defun indent-line-to (column)
  88.   "Indent current line to COLUMN.
  89. This function removes or adds spaces and tabs at beginning of line
  90. only if necessary.  It leaves point at end of indentation."
  91.   (back-to-indentation)
  92.   (let ((cur-col (current-column)))
  93.     (cond ((< cur-col column)
  94.        (if (> (- column (* (/ cur-col tab-width) tab-width)) tab-width)
  95.            (delete-region (point)
  96.                   (progn (skip-chars-backward " ") (point))))
  97.        (indent-to column))
  98.       ((> cur-col column) ; too far right (after tab?)
  99.        (delete-region (progn (move-to-column column t) (point))
  100.               (progn (back-to-indentation) (point)))))))
  101.  
  102. (defun current-left-margin ()
  103.   "Return the left margin to use for this line.
  104. This is the value of the buffer-local variable `left-margin' plus the value
  105. of the `left-margin' text-property at the start of the line."
  106.   (save-excursion
  107.     (back-to-indentation)
  108.     (max 0
  109.      (+ left-margin (or (get-text-property
  110.                  (if (and (eobp) (not (bobp)))
  111.                  (1- (point)) (point))
  112.                  'left-margin) 0)))))
  113.  
  114. (defun move-to-left-margin (&optional n force)
  115.   "Move to the left margin of the current line.
  116. With optional argument, move forward N-1 lines first.
  117. The column moved to is the one given by the `current-left-margin' function.
  118. If the line's indentation appears to be wrong, and this command is called
  119. interactively or with optional argument FORCE, it will be fixed."
  120.   (interactive (list (prefix-numeric-value current-prefix-arg) t))
  121.   (beginning-of-line n)
  122.   (skip-chars-forward " \t")
  123.   (let ((lm (current-left-margin))
  124.     (cc (current-column)))
  125.     (cond ((> cc lm)
  126.        (if (> (move-to-column lm force) lm)
  127.            ;; If lm is in a tab and we are not forcing, move before tab
  128.            (backward-char 1)))
  129.       ((and force (< cc lm))
  130.        (indent-to-left-margin)))))
  131.  
  132. ;; This is the default indent-line-function,
  133. ;; used in Fundamental Mode, Text Mode, etc.
  134. (defun indent-to-left-margin ()
  135.   "Indent current line to the column given by `current-left-margin'."
  136.   (indent-line-to (current-left-margin)))
  137.  
  138. (defun delete-to-left-margin (&optional from to)
  139.   "Remove left margin indentation from a region.
  140. This deletes to the column given by `current-left-margin'.
  141. In no case will it delete non-whitespace.
  142. Args FROM and TO are optional; default is the whole buffer."
  143.   (save-excursion
  144.     (goto-char (or to (point-max)))
  145.     (setq to (point-marker))
  146.     (goto-char (or from (point-min)))
  147.     (or (bolp) (forward-line 1))
  148.     (while (< (point) to)
  149.       (delete-region (point) (progn (move-to-left-margin nil t) (point)))
  150.       (forward-line 1))
  151.     (move-marker to nil)))
  152.  
  153. (defun set-left-margin (from to lm)
  154.   "Set the left margin of the region to WIDTH.
  155. If `auto-fill-mode' is active, re-fill the region to fit the new margin."
  156.   (interactive "r\nNSet left margin to column: ")
  157.   (if (interactive-p) (setq lm (prefix-numeric-value lm)))
  158.   (save-excursion
  159.     ;; If inside indentation, start from BOL.
  160.     (goto-char from)
  161.     (skip-chars-backward " \t")
  162.     (if (bolp) (setq from (point)))
  163.     ;; Place end after whitespace
  164.     (goto-char to)
  165.     (skip-chars-forward " \t")
  166.     (setq to (point-marker)))
  167.   ;; Delete margin indentation first, but keep paragraph indentation.
  168.   (delete-to-left-margin from to)
  169.   (put-text-property from to 'left-margin lm)
  170.   (indent-rigidly from to lm)
  171.   (if auto-fill-function (save-excursion (fill-region from to nil t t)))
  172.   (move-marker to nil))
  173.  
  174. (defun set-right-margin (from to lm)
  175.   "Set the right margin of the region to WIDTH.
  176. If `auto-fill-mode' is active, re-fill the region to fit the new margin."
  177.   (interactive "r\nNSet right margin to width: ")
  178.   (if (interactive-p) (setq lm (prefix-numeric-value lm)))
  179.   (save-excursion
  180.     (goto-char from)
  181.     (skip-chars-backward " \t")
  182.     (if (bolp) (setq from (point))))
  183.   (put-text-property from to 'right-margin lm)
  184.   (if auto-fill-function (save-excursion (fill-region from to nil t t))))
  185.  
  186. (defun alter-text-property (from to prop func &optional object)
  187.   "Programmatically change value of a text-property.
  188. For each region between FROM and TO that has a single value for PROPERTY,
  189. apply FUNCTION to that value and sets the property to the function's result.
  190. Optional fifth argument OBJECT specifies the string or buffer to operate on."
  191.   (let ((begin from)
  192.     end val)
  193.     (while (setq val (get-text-property begin prop object)
  194.          end (text-property-not-all begin to prop val object))
  195.       (put-text-property begin end prop (funcall func val) object)
  196.       (setq begin end))
  197.     (if (< begin to)
  198.     (put-text-property begin to prop (funcall func val) object))))
  199.  
  200. (defun increase-left-margin (from to inc)
  201.   "Increase or decrease the left-margin of the region.
  202. With no prefix argument, this adds `standard-indent' of indentation.
  203. A prefix arg (optional third arg INC noninteractively) specifies the amount
  204. to change the margin by, in characters.
  205. If `auto-fill-mode' is active, re-fill the region to fit the new margin."
  206.   (interactive "*r\nP")
  207.   (setq inc (if inc (prefix-numeric-value inc) standard-indent))
  208.   (save-excursion
  209.     (goto-char from)
  210.     (skip-chars-backward " \t")
  211.     (if (bolp) (setq from (point)))
  212.     (goto-char to)
  213.     (setq to (point-marker)))
  214.   (alter-text-property from (marker-position to) 'left-margin ; XEmacs
  215.                (lambda (v) (max (- left-margin) (+ inc (or v 0)))))
  216.   (indent-rigidly from (marker-position to) inc) ; XEmacs
  217.   (if auto-fill-function
  218.       (save-excursion
  219.     (fill-region from (marker-position to) nil t t))) ; XEmacs
  220.   (move-marker to nil))
  221.  
  222. (defun decrease-left-margin (from to inc)
  223.   "Make the left margin of the region smaller.
  224. With no prefix argument, decrease the indentation by `standard-indent'.
  225. A prefix arg (optional third arg INC noninteractively) specifies the amount
  226. to change the margin by, in characters.
  227. If `auto-fill-mode' is active, re-fill the region to fit the new margin."
  228.   (interactive "*r\nP")
  229.   (setq inc (if inc (prefix-numeric-value inc) standard-indent))
  230.   (increase-left-margin from to (- inc)))
  231.  
  232. (defun increase-right-margin (from to inc)
  233.   "Increase the right-margin of the region.
  234. With no prefix argument, increase the right margin by `standard-indent'.
  235. A prefix arg (optional third arg INC noninteractively) specifies the amount
  236. to change the margin by, in characters.  A negative argument decreases
  237. the right margin width.
  238. If `auto-fill-mode' is active, re-fill the region to fit the new margin."
  239.   (interactive "r\nP")
  240.   (if (interactive-p)
  241.       (setq inc (if inc (prefix-numeric-value current-prefix-arg)
  242.           standard-indent)))
  243.   (save-excursion
  244.     (alter-text-property from to 'right-margin
  245.        (lambda (v) (+ inc (or v 0))))
  246.     (if auto-fill-function
  247.     (fill-region from to nil t t))))
  248.  
  249. (defun decrease-right-margin (from to inc)
  250.   "Make the right margin of the region smaller.
  251. With no prefix argument, decrease the right margin by `standard-indent'.
  252. A prefix arg (optional third arg INC noninteractively) specifies the amount
  253. of width to remove, in characters.  A negative argument increases
  254. the right margin width.
  255. If `auto-fill-mode' is active, re-fills region to fit in new margin."
  256.   (interactive "*r\nP")
  257.   (setq inc (if inc (prefix-numeric-value inc) standard-indent))
  258.   (increase-right-margin from to (- inc)))
  259.  
  260. (defun beginning-of-line-text (&optional n)
  261.   "Move to the beginning of the text on this line.
  262. With optional argument, move forward N-1 lines first.
  263. From the beginning of the line, moves past the left-margin indentation, the
  264. fill-prefix, and any indentation used for centering or right-justifying the
  265. line, but does not move past any whitespace that was explicitly inserted 
  266. \(such as a tab used to indent the first line of a paragraph)."
  267.   (interactive "p")
  268.   (beginning-of-line n)
  269.   (skip-chars-forward " \t")
  270.   ;; Skip over fill-prefix.
  271.   (if (and fill-prefix 
  272.        (not (string-equal fill-prefix "")))
  273.       (if (equal fill-prefix
  274.          (buffer-substring 
  275.           (point) (min (point-max) (+ (length fill-prefix) (point)))))
  276.       (forward-char (length fill-prefix)))
  277.     (if (and adaptive-fill-mode adaptive-fill-regexp
  278.          (looking-at adaptive-fill-regexp))
  279.     (goto-char (match-end 0))))
  280.   ;; Skip centering or flushright indentation
  281.   (if (memq (current-justification) '(center right))
  282.       (skip-chars-forward " \t")))
  283.  
  284. (defvar indent-region-function nil
  285.   "Short cut function to indent region using `indent-according-to-mode'.
  286. A value of nil means really run `indent-according-to-mode' on each line.")
  287.  
  288. (defun indent-region (start end column)
  289.   "Indent each nonblank line in the region.
  290. With no argument, indent each line using `indent-according-to-mode',
  291. or use `indent-region-function' to do the whole region if that's non-nil.
  292. If there is a fill prefix, make each line start with the fill prefix.
  293. With argument COLUMN, indent each line to that column.
  294. Called from a program, takes three args: START, END and COLUMN."
  295.   (interactive "r\nP")
  296.   (if (null column)
  297.       (if fill-prefix
  298.       (save-excursion
  299.         (goto-char end)
  300.         (setq end (point-marker))
  301.         (goto-char start)
  302.         (let ((regexp (regexp-quote fill-prefix)))
  303.         (while (< (point) end)
  304.           (or (looking-at regexp)
  305.                   (and (bolp) (eolp))
  306.           (insert fill-prefix))
  307.           (forward-line 1))))
  308.     (if indent-region-function
  309.         (funcall indent-region-function start end)
  310.       (save-excursion
  311.       (goto-char end)
  312.       (setq end (point-marker))
  313.       (goto-char start)
  314.       (or (bolp) (forward-line 1))
  315.       (while (< (point) end)
  316.             (or (and (bolp) (eolp))
  317.                 (funcall indent-line-function))
  318.         (forward-line 1))
  319.       (move-marker end nil))))
  320.     (setq column (prefix-numeric-value column))
  321.     (save-excursion
  322.       (goto-char end)
  323.       (setq end (point-marker))
  324.       (goto-char start)
  325.       (or (bolp) (forward-line 1))
  326.       (while (< (point) end)
  327.     (delete-region (point) (progn (skip-chars-forward " \t") (point)))
  328.     (or (eolp)
  329.         (indent-to column 0))
  330.     (forward-line 1))
  331.       (move-marker end nil))))
  332.  
  333. (defun indent-relative-maybe ()
  334.   "Indent a new line like previous nonblank line."
  335.   (interactive)
  336.   (indent-relative t))
  337.  
  338. (defun indent-relative (&optional unindented-ok)
  339.   "Space out to under next indent point in previous nonblank line.
  340. An indent point is a non-whitespace character following whitespace.
  341. If the previous nonblank line has no indent points beyond the
  342. column point starts at, `tab-to-tab-stop' is done instead."
  343.   (interactive "P")
  344.   (if abbrev-mode (expand-abbrev))
  345.   (let ((start-column (current-column))
  346.     indent)
  347.     (save-excursion
  348.       (beginning-of-line)
  349.       (if (re-search-backward "^[^\n]" nil t)
  350.       (let ((end (save-excursion (forward-line 1) (point))))
  351.         (move-to-column start-column)
  352.         ;; Is start-column inside a tab on this line?
  353.         (if (> (current-column) start-column)
  354.         (backward-char 1))
  355.         (or (looking-at "[ \t]")
  356.         unindented-ok
  357.         (skip-chars-forward "^ \t" end))
  358.         (skip-chars-forward " \t" end)
  359.         (or (= (point) end) (setq indent (current-column))))))
  360.     (if indent
  361.     (let ((opoint (point-marker)))
  362.       (delete-region (point) (progn (skip-chars-backward " \t") (point)))
  363.       (indent-to indent 0)
  364.       (if (> opoint (point))
  365.           (goto-char opoint))
  366.       (move-marker opoint nil))
  367.       (tab-to-tab-stop))))
  368.  
  369. (defvar tab-stop-list
  370.   '(8 16 24 32 40 48 56 64 72 80 88 96 104 112 120)
  371.   "*List of tab stop positions used by `tab-to-tab-stops'.
  372. This should be a list of integers, ordered from smallest to largest.")
  373.  
  374. (defvar edit-tab-stops-map nil "Keymap used in `edit-tab-stops'.")
  375. (if edit-tab-stops-map
  376.     nil
  377.   (setq edit-tab-stops-map (make-sparse-keymap))
  378.   (define-key edit-tab-stops-map "\C-x\C-s" 'edit-tab-stops-note-changes)
  379.   (define-key edit-tab-stops-map "\C-c\C-c" 'edit-tab-stops-note-changes))
  380.  
  381. (defvar edit-tab-stops-buffer nil
  382.   "Buffer whose tab stops are being edited--in case
  383. the variable `tab-stop-list' is local in that buffer.")
  384.  
  385. (defun edit-tab-stops ()
  386.   "Edit the tab stops used by `tab-to-tab-stop'.
  387. Creates a buffer *Tab Stops* containing text describing the tab stops.
  388. A colon indicates a column where there is a tab stop.
  389. You can add or remove colons and then do \\<edit-tab-stops-map>\\[edit-tab-stops-note-changes] to make changes take effect."
  390.   (interactive)
  391.   (setq edit-tab-stops-buffer (current-buffer))
  392.   (switch-to-buffer (get-buffer-create "*Tab Stops*"))
  393.   ;; #### I18N3 should mark buffer as output-translating
  394.   (use-local-map edit-tab-stops-map)
  395.   (make-local-variable 'indent-tabs-mode)
  396.   (setq indent-tabs-mode nil)
  397.   (overwrite-mode 1)
  398.   (setq truncate-lines t)
  399.   (erase-buffer)
  400.   (let ((tabs tab-stop-list))
  401.     (while tabs
  402.       (indent-to (car tabs) 0)
  403.       (insert ?:)
  404.       (setq tabs (cdr tabs))))
  405.   (let ((count 0))
  406.     (insert ?\n)
  407.     (while (< count 8)
  408.       (insert (+ count ?0))
  409.     (insert "         ")
  410.       (setq count (1+ count)))
  411.     (insert ?\n)
  412.     (while (> count 0)
  413.       (insert "0123456789")
  414.       (setq count (1- count))))
  415.   ;; XEmacs
  416.   (insert (substitute-command-keys "\nTo install changes, type \\<edit-tab-stops-map>\\[edit-tab-stops-note-changes]"))
  417.   (goto-char (point-min)))
  418.  
  419. (defun edit-tab-stops-note-changes ()
  420.   "Put edited tab stops into effect."
  421.   (interactive)
  422.     (let (tabs)
  423.       (save-excursion
  424.     (goto-char 1)
  425.     (end-of-line)
  426.     (while (search-backward ":" nil t)
  427.       (setq tabs (cons (current-column) tabs))))
  428.       (bury-buffer (prog1 (current-buffer)
  429.               (switch-to-buffer edit-tab-stops-buffer)))
  430.       (setq tab-stop-list tabs))
  431.   (message "Tab stops installed"))
  432.  
  433. (defun tab-to-tab-stop ()
  434.   "Insert spaces or tabs to next defined tab-stop column.
  435. The variable `tab-stop-list' is a list of columns at which there are tab stops.
  436. Use \\[edit-tab-stops] to edit them interactively."
  437.   (interactive)
  438.   (and abbrev-mode (eq (char-syntax (char-before (point))) ?w)
  439.        (expand-abbrev))
  440.   (let ((tabs tab-stop-list))
  441.     (while (and tabs (>= (current-column) (car tabs)))
  442.       (setq tabs (cdr tabs)))
  443.     (if tabs
  444.     (let ((opoint (point)))
  445.       (skip-chars-backward " \t")
  446.       (delete-region (point) opoint)
  447.       (indent-to (car tabs)))
  448.       (insert ?\ ))))
  449.  
  450. (defun move-to-tab-stop ()
  451.   "Move point to next defined tab-stop column.
  452. The variable `tab-stop-list' is a list of columns at which there are tab stops.
  453. Use \\[edit-tab-stops] to edit them interactively."
  454.   (interactive)
  455.   (let ((tabs tab-stop-list))
  456.     (while (and tabs (>= (current-column) (car tabs)))
  457.       (setq tabs (cdr tabs)))
  458.     (if tabs
  459.     (let ((before (point)))
  460.       (move-to-column (car tabs) t)
  461.       (save-excursion
  462.         (goto-char before)
  463.         ;; If we just added a tab, or moved over one,
  464.         ;; delete any superfluous spaces before the old point.
  465.         (if (and (eq (char-before (point)) ?\ )
  466.              (eq (char-after (point)) ?\t))
  467.         (let ((tabend (* (/ (current-column) tab-width) tab-width)))
  468.           (while (and (> (current-column) tabend)
  469.                   (eq (char-before (point)) ?\ ))
  470.             (forward-char -1))
  471.           (delete-region (point) before))))))))
  472.  
  473. ;(define-key global-map "\t" 'indent-for-tab-command)
  474. ;(define-key esc-map "\034" 'indent-region)
  475. ;(define-key ctl-x-map "\t" 'indent-rigidly)
  476. ;(define-key esc-map "i" 'tab-to-tab-stop)
  477.  
  478. ;;; indent.el ends here
  479.